package com.santi.framework.dictionary.impl;

import com.santi.framework.common.constants.CommonConstants;
import com.santi.framework.common.helper.WordHelper;
import com.santi.framework.dictionary.service.Dictionary;
import com.santi.framework.dictionary.service.DictionaryService;
import com.santi.framework.dictionary.vo.WordVo;
import com.santi.framework.filemanager.impl.FileReaderImpl;
import com.santi.framework.util.StringUtil;
import com.santi.framework.util.TokenReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class DictionaryImpl implements DictionaryService {

    private Map<String, WordVo> wordMap = null;

    public  DictionaryImpl(String fileName, String langCode) throws Exception {
        wordMap =
                new HashMap<String, WordVo>();
        loadDictionary(fileName, langCode);
    }
    /*
     * (non-Javadoc)
     * 
     * @see
     * com.santi.framework.dictionary.service.DictionaryService#addWord(com.
     * santi.framework.dictionary.vo.WordVo)
     */

    @Override
    public boolean addWord(WordVo word) {
        // TODO Auto-generated method stub
        return false;
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.santi.framework.dictionary.service.DictionaryService#isValidWord(
     * com.santi.framework.dictionary.vo.WordVo)
     */
    @Override
    public boolean contains(String searchWord) {
        // TODO Auto-generated method stub
        if (!StringUtil.isNullOrBlank(searchWord) && wordMap.containsKey(searchWord)) {

            return true;

        }
        return false;
    }

    /*
   

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.santi.framework.dictionary.service.DictionaryService#loadDictionary
     * (java.lang.String)
     */
    private void loadDictionary(String fileName, String langCode) throws Exception {
        // TODO Auto-generated method stub
        FileReaderImpl fileReaderImpl = new FileReaderImpl(fileName);
        for (String line : fileReaderImpl) {
           
          //  System.out.println(".......line........"+line);
            String[] wordsRecord = TokenReader.readTokens(
                    (line), CommonConstants.FIELD_SAPARATOR);
            WordVo wordVo = WordHelper.populateWordVo(wordsRecord, langCode);
            if (null != wordVo) {
                wordMap.put(wordVo.getRootWord(), wordVo);
            }
        }

    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * com.santi.framework.dictionary.service.DictionaryService#saveDictionary
     * (com.santi.framework.dictionary.service.Dictionary, java.lang.String)
     */
    @Override
    public void saveWord(Dictionary dictionary, String filename) {
        // TODO Auto-generated method stub
    }

    @Override
    public List<String> getWordList() {
        List<String> wordList=null;
        if(!wordMap.isEmpty()){
           wordList = new ArrayList<String>();            
           Iterator ir= wordMap.keySet().iterator();
           while(ir.hasNext()){
            String word=(String)ir.next();
            wordList.add(word);
           }
          Collections.sort(wordList);
        }
        
        return wordList;
        // TODO Auto-generated method stub

    }

    @Override
    public WordVo getWordVo(String searchWord) {

        if (!StringUtil.isNullOrBlank(searchWord)&& wordMap.containsKey(searchWord)) {
            return wordMap.get(searchWord);
        } else {
            return null;
        }

    }

    @Override
    public int getDictionarySize() {
       return wordMap.size();
    }
}
